package cn.lizemin.demo.thread.threadPool03;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class ScheduledExecutorServiceDemo02 {
    //这是一个main方法,是程序的入口
    public static void main(String[] args) {
        //1.创建一个具备延迟执行任务的线程池对象
        ScheduledExecutorService es = Executors.newScheduledThreadPool(3, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                int n = 1;
                return new Thread(r,"自定义线程："+n++);

            }
        });
        //2.创建多个任务对象，提交任务，每个任务延迟2秒执行
        //初始间隔1秒，每间隔2秒执行一次任务。
        es.scheduleAtFixedRate(new MyRunnable2(1),1,2,TimeUnit.SECONDS);
        System.out.println("over");


    }

}

class MyRunnable2 implements Runnable{
    private int id ; //

    public MyRunnable2(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name+"执行了任务："+id);

    }


}

